library(tidyverse)
library(cepespR)
library(readxl)
library(data.table)
library(stargazer)
library(haven)

#usar os arquivos "Estadual" e "Federal" na pasta




load("Federal.RData")
load("Estadual.RData")

#modelo de regressão deputado federal

FN_f$M70<-ifelse(FN_f$UF=="SP","1","0")
FN_f$M53<-ifelse(FN_f$UF=="MG","1","0")
FN_f$M46<-ifelse(FN_f$UF=="RJ","1","0")
FN_f$M39<-ifelse(FN_f$UF=="BA","1","0")
FN_f$M30<-ifelse(FN_f$UF=="RS"|FN_f$UF=="PR","1","0")
FN_f$M20<-ifelse(FN_f$UF=="PE"|FN_f$UF=="CE","1","0")
FN_f$M10<-ifelse(FN_f$UF=="MA"|FN_f$UF=="GO"|FN_f$UF=="PA"|FN_f$UF=="SC"|FN_f$UF=="PB"|FN_f$UF=="ES"|FN_f$UF=="PI","1","0")
FN_f$M9<-ifelse(FN_f$UF=="AL"|FN_f$UF=="AC"|FN_f$UF=="AM"|FN_f$UF=="AP"|FN_f$UF=="MS"|FN_f$UF=="MT"|FN_f$UF=="RN"|FN_f$UF=="RO"|FN_f$UF=="RR"|FN_f$UF=="SE"|FN_f$UF=="TO","1","0")

Dist100<-filter(FN_f,Distancia<=100)
Dist200<-filter(FN_f,Distancia<=200)
Dist300<-filter(FN_f,Distancia<=300)
Dist500<-filter(FN_f,Distancia<=500)


R_Dist100<-lm(Porcentagem~Distancia+incubente+experi_munic_est+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M53+M46+M39+M30+M20+M10+M9,Dist100)
R_Dist200<-lm(Porcentagem~Distancia+incubente+experi_munic_est+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M53+M46+M39+M30+M20+M10+M9,Dist200)
R_Dist300<-lm(Porcentagem~Distancia+incubente+experi_munic_est+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M53+M46+M39+M30+M20+M10+M9,Dist300)
R_Dist500<-lm(Porcentagem~Distancia+incubente+experi_munic_est+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M53+M46+M39+M30+M20+M10+M9,Dist500)

stargazer(R_Dist100,R_Dist200,R_Dist300,R_Dist500,title="Results",align=TRUE, type= "text",out ="Tabela4_F.txt")

#modelo de regressão deputado estadual

FN_e$M94<-ifelse(FN_e$UF=="SP","1","0")
FN_e$M77<-ifelse(FN_e$UF=="MG","1","0")
FN_e$M70<-ifelse(FN_e$UF=="RJ","1","0")
FN_e$M63<-ifelse(FN_e$UF=="BA","1","0")
FN_e$M50<-ifelse(FN_e$UF=="RS"|FN_e$UF=="PR"|FN_e$UF=="PE","1","0")
FN_e$M40<-ifelse(FN_e$UF=="CE"|FN_e$UF=="MA"|FN_e$UF=="GO"|FN_e$UF=="PA"|FN_e$UF=="SC","1","0")
FN_e$M30<-ifelse(FN_e$UF=="PB"|FN_e$UF=="ES"|FN_e$UF=="PI","1","0")
FN_e$M20<-ifelse(FN_e$UF=="AL"|FN_e$UF=="AC"|FN_e$UF=="AM"|FN_e$UF=="AP"|FN_e$UF=="MS"|FN_e$UF=="MT"|FN_e$UF=="RN"|FN_e$UF=="RO"|FN_e$UF=="RR"|FN_e$UF=="SE"|FN_e$UF=="TO","1","0")


Dist100<-filter(FN_e,Distancia<=100)
Dist200<-filter(FN_e,Distancia<=200)
Dist300<-filter(FN_e,Distancia<=300)
Dist500<-filter(FN_e,Distancia<=500)


R_Dist100<-lm(Porcentagem~Distancia+incubente+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M77+M70+M63+M50+M40+M30+M20,Dist100)
R_Dist200<-lm(Porcentagem~Distancia+incubente+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M77+M70+M63+M50+M40+M30+M20,Dist200)
R_Dist300<-lm(Porcentagem~Distancia+incubente+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M77+M70+M63+M50+M40+M30+M20,Dist300)
R_Dist500<-lm(Porcentagem~Distancia+incubente+experi_munic+Despesa+genero+ANO2014+ANO2018+Centro+Esquerda+capitais+M77+M70+M63+M50+M40+M30+M20,Dist500)

stargazer(R_Dist100,R_Dist200,R_Dist300,R_Dist500,title="Results",align=TRUE, type= "text",out ="Tabela5_E.txt")





